package com.sto.stoapp.thread;

import java.io.File;

import com.sto.stoapp.db.manager.DeleteDataManager;
import com.sto.stoapp.util.FileUtil;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

public class DeleteSdFileThread extends BaseThread {
	int MINI_SD_MEMORY = 100;

	public DeleteSdFileThread(Context context) {
		super(context);
	}

	@Override
	public void run() {

		super.run();
		try {
			if (readSDCard() <= MINI_SD_MEMORY) {
				FileUtil.deleteFile(FileUtil.SDPATH + "crash");
				FileUtil.deleteFile(FileUtil.SDPATH + "sto" + FileUtil.SEP
						+ "log");
				DeleteDataManager.getInstance(context).deleteRecordData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 返回M
	private int readSDCard() {
		int sd = 0;
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			File sdcardDir = Environment.getExternalStorageDirectory();
			StatFs sf = new StatFs(sdcardDir.getPath());
			long blockSize = sf.getBlockSize();
			long blockCount = sf.getBlockCount();
			long availCount = sf.getAvailableBlocks();
			Log.d("sd", "block大小:" + blockSize + ",block数目:" + blockCount
					+ ",总大小:" + blockSize * blockCount / 1024 + "KB");
			Log.d("sd", "可用的block数目：:" + availCount + ",剩余空间:" + availCount
					* blockSize / 1024 + "KB");
			sd = (int) ((availCount * blockSize / 1024) / 1024);

		}
		return sd;
	}
}
